package cn.zxyh.controller;

import cn.zxyh.pojo.Bill;
import cn.zxyh.pojo.BillDTO;
import cn.zxyh.pojo.User;
import cn.zxyh.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    RedisTemplate redisTemplate;
    @Resource
    BillService billService;

    /**
     * 通过登录者的状态进行查询账单信息
     * @param paraMap
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public Map<String, Object> list(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        String type = request.getParameter("type");
        int typeInt = 0;
        if (type == null) {
            map.put("code", 1);
            map.put("msg", "类型错误");
            return map;
        }
        if (type.equals("phone")) {
            typeInt = 0;
        } else if (type.equals("life")) {
            typeInt = 1;
        } else {
            typeInt = 2;
        }
        int status = user.getStatus();
        List<Bill> list = null;
        List<BillDTO> billDTOList = new ArrayList<>();      //该值最终返回给前台
        //根据用户的状态（管理员或者普通用户）返回账单列表
        list = status == 1 ? billService.findBillAll() : billService.findByUser(user,typeInt);
        //将后台数据遍历给DTO
        for (Bill b: list
             ) {
            BillDTO billDTO = new BillDTO();
            billDTO.setBillId(String.valueOf(b.getId()));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String d = simpleDateFormat.format(b.getGeneBillTime());
            billDTO.setBillDate(d);
            billDTO.setMoney(String.valueOf(b.getMoney()));
            if (b.getStatus() == 0) {
                billDTO.setBillStatus("未支付");
            } else {
                billDTO.setBillStatus("已支付");
            }


            billDTO.setWayOfPay("中信银行信用卡");
            b.setWayOfPayment("中信银行信用卡");
            String pt = "";
            if (b.getPaymentTime() != null) {
                pt =  simpleDateFormat.format(b.getPaymentTime());
            }else billDTO.setWayOfPay("");

            billDTO.setPayDate(pt);
            billDTO.setReceiver(b.getReceiver());
            if (type.equals("phone")) {
                billDTO.setBillName("电话账单");
            } else if (type.equals("life")) {
                billDTO.setBillName("水电账单");
            } else {
                billDTO.setBillName("信用卡账单");
            }
            billDTOList.add(billDTO);
        }
        map.put("code", "0");
        map.put("data", billDTOList);
        return map;
    }

    /**
     * 根据账单号进行付款
     * @return
     */
    @RequestMapping("/pay")
    public Map<String,Object> payOrder(@RequestBody Map<String,String> paraMap){
        Map<String, Object> map = new HashMap<>();
        String temp=paraMap.get("billId");
        String password=paraMap.get("password");
        int billid = Integer.parseInt(temp);
        int status = billService.payBill(billid,password);
        if(status==0){
            map.put("code",0);
            return map;
        }else if(status==1){
            map.put("code",1);
            map.put("msg","余额不足");
            return map;
        }else{
            map.put("code",2);
            map.put("msg","支付密码错误");
            return map;
        }
    }
}
